package services;

import java.util.Collection;
import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import repositories.OrderRepository;
import domain.Customer;
import domain.Money;
import domain.Order;
import domain.PurchasedItem;
import domain.ShoppingCartItemUnit;

@Service
@Transactional
public class OrderService {

	@Autowired
	private OrderRepository orderRepository;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private PurchasedItemService purchasedItemService;
	@Autowired
	private AvailableItemService availableItemService;
	@Autowired
	private ShoppingCartItemUnitService shoppingCartItemUnitService;

	// Constructors
	public OrderService() {
		super();
	}

	// Simple CRUD methods
	public Order create() {
		Customer principal;
		Order res;
		Date currentMoment;
		Money totalPrice;

		totalPrice = new Money();
		totalPrice.setAmount(0.0);
		totalPrice.setCurrency("euros");
		currentMoment = new Date();
		principal = customerService.findByPrincipal();
		res = new Order();

		res.setCreationMoment(currentMoment);
		res.setBuyer(principal);
		res.setTotalPrice(totalPrice);

		return res;
	}

	public Order save(Order order) {
		Assert.notNull(order);
		Order res;

		res = orderRepository.save(order);

		return res;
	}

	public Collection<Order> findAll() {
		Collection<Order> result;
		result = orderRepository.findAll();
		return result;
	}

	public Order findOne(int orderId) {
		Order result;

		result = orderRepository.findOne(orderId);

		return result;
	}

	// Other Business Methods
	public Order addItemsToOrder() {
		Order order;
		
		order=create();
		order= save(order);
		Assert.notNull(order);
		Order res;
		Collection<ShoppingCartItemUnit> cart;
		PurchasedItem p;
		Double totalAmount;
		Money totalPrice;

		totalPrice = new Money();
		cart = shoppingCartItemUnitService.findByCustomer();
		totalAmount = 0.0;

		for (ShoppingCartItemUnit s : cart) {

			Assert.isTrue(s.getAvailableItem().getAvailableUnits() >= s
					.getNumberOfItems());
			p = purchasedItemService.create(s, order);
			purchasedItemService.save(p);
			totalAmount = totalAmount + p.getPurchasedUnits()
					* p.getPrice().getAmount();
			s.getAvailableItem().setAvailableUnits(
					s.getAvailableItem().getAvailableUnits()
							- p.getPurchasedUnits());
			availableItemService.saveUnits(s.getAvailableItem());
		}

		totalPrice.setAmount(totalAmount);
		totalPrice.setCurrency("euros");
		order.setTotalPrice(totalPrice);
		res = save(order);

		availableItemService.emptyCart();

		return res;
	}

	public Double findTotalAmount() {
		Double res;
		res = orderRepository.findTotalAmount();
		return res;

	}

	public Collection<Order> findBoughtOrders() {
		Collection<Order> orders;
		Customer customer;

		customer = customerService.findByPrincipal();
		orders = orderRepository.findByBuyerId(customer.getId());

		return orders;
	}

}
